import router from "@/router"

/**
 * @description 路由跳转
 * */
export function toPath(path: string, query = {}) {
  router.push({path, query})
}

/**
 * @description 路由跳转name
 * */
export function toName(name: string, params = {}) {
  router.push({name, params})
}

/**
 * @description 路由跳转打开新页面
 * */
export function toNewPath(path: string, query = {}) {
  let {href} = router.resolve({
    path,
    query
  });
  window.open(href, "_blank");
}

/**
 * @description 打开新页面
 * */
export function toOtherPath(path: string) {
  window.open(path, "_blank");
}

/**
 * @description 路由跳转返回上一页
 * */
export function toPreviousPath() {
  router.go(-1)
}

/**
 * @description 对于两个对象中拥有相同的键，获取后面键的属性值给前面键并且不改变前面obj的结构(一层)
 * */
export function deconstructionObj(objFirst: any, objLast: any) {
  for (const e in objFirst) {
    objLast[e] !== undefined && (objFirst[e] = objLast[e])
  }
}

/**
 * @description blob下载excel文档
 * */
export function handleBlob(data: BlobPart, fileNameParam = "产品列表") {
  const blob = new Blob([data]);
  const fileName = fileNameParam + ".xlsx";
  const elink = document.createElement("a");
  elink.download = fileName;
  elink.style.display = "none";
  elink.href = URL.createObjectURL(blob);
  document.body.appendChild(elink);
  elink.click();
  URL.revokeObjectURL(elink.href); // 释放URL 对象
  document.body.removeChild(elink);
}

// 解包proxy（echarts需要用到）
export function unWarp(obj: any) {
  return obj && (obj.__v_raw || obj.valueOf() || obj)
}
